package com.boss.salary.controller;


import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.framework.exception.AppException;
import com.boss.salary.dto.TodoDataParam;
import com.boss.salary.service.IRedisService;
import com.boss.salary.service.ISalary2OutsideService;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.utils.SysUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author hao
 * @date 2023/4/11 15:05
 */
@RestController
@RequestMapping("/v1")
public class Salary2OutsideController extends BasicinfoBaseController {
    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(Salary2OutsideController.class);
    @Autowired
    private ISalary2OutsideService iHomeServices;
    @Autowired
    private IRedisService redisService;
    @PostMapping("/task/process")
    public ApiResultDTO getAuditCount(@RequestHeader("tokenid") String tokenid,
                                      @RequestBody TodoDataParam param) {
        try {
                if (StringUtils.isEmpty(param.getUserguid())) {
                    throw new AppException("用户id不能为空");
                }
                if (StringUtils.isEmpty(param.getRoleguid())) {
                    throw new AppException("角色guid不能为空");
                }
                if (StringUtils.isEmpty(param.getProvince())) {
                    throw new AppException("区划不能为空");
                }
                if (StringUtils.isEmpty(param.getYear())) {
                    throw new AppException("年度不能为空");
                }
                Object o =redisService.get("salaryTodoTask"+tokenid);
                if(!SysUtils.isNullOrEmpty(o)){
                    return  ApiResultDTO.success(o,"查询成功");
                }
                UserDTO userDTO = getUser(tokenid);
//                logger.info("查询业务系统的代办入参数据: userguid={},province={},year={},tonkenid={},param={}",param.getUserguid(),param.getProvince(),param.getYear(),tokenid,param);
                List<Map<String,Object>> taskInfoList = iHomeServices.getFlowTaskData(userDTO,tokenid,param.getConfigs(), param.getUserguid(),param.getRoleguid(),param.getProvince(), param.getYear());
//                logger.info("查询业务系统的代办数据返回值: {}", JSON.toJSON(taskInfoList));
            redisService.put("salaryTodoTask"+tokenid,taskInfoList);
                return  ApiResultDTO.success(taskInfoList,"查询成功");
            } catch (Exception e) {
                logger.error("查询代办异常:{}", e.getMessage());
                return  ApiResultDTO.error(ERROR_RSCODE, "查询异常",e.getMessage());

            }
}
}
